{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 面向对象：将对象抽象代码结构，例，鸟类class：大小，重量，动作：叫，飞等\n",
    "## 面向过程：用代码实现你需的的操作，例如变量定义、数据计算等。。。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类的语法："
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "class 类名(object):\n",
    "    属性1\n",
    "    属性2\n",
    "    属性3\n",
    "    ...\n",
    "    \n",
    "    操作1（方法）\n",
    "    操作2（方法）\n",
    "    操作3（方法）\n",
    "    ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Bird(object):\n",
    "    weight = 20\n",
    "    height = 100\n",
    "    \n",
    "    def cry(self):   # 类里面的方法都需要传self参数，使用对象来表明自身。\n",
    "        print('emmmmm....')\n",
    "        \n",
    "    def talk(self):\n",
    "        print('身高：' + str(self.height), \", 体重：\" + str(self.weight))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类初始化成对象,调用对象的属性和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "bird = Bird()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.Bird object at 0x0000000004F6EAC8>\n"
     ]
    }
   ],
   "source": [
    "print(bird)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bird.weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bird.height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "emmmmm....\n"
     ]
    }
   ],
   "source": [
    "bird.cry()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "身高：100 , 体重：20\n"
     ]
    }
   ],
   "source": [
    "bird.talk()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类的初始化的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Bird(object):\n",
    "    weight = 20\n",
    "    height = 100\n",
    "    \n",
    "    def __init__(self):\n",
    "        pass  #语法占位，什么都不做\n",
    "    \n",
    "    def cry(self):   # 类里面的方法都需要传self参数，使用对象来表明自身。\n",
    "        print('emmmmm....')\n",
    "        \n",
    "    def talk(self):\n",
    "        print('身高：' + str(self.height), \", 体重：\" + str(self.weight))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Bird(object):\n",
    "\n",
    "    def __init__(self, height, weight):  #重构初始化函数\n",
    "        self.height = height\n",
    "        self.weight = weight\n",
    "    \n",
    "    def cry(self):\n",
    "        print('emmmmm....')\n",
    "        \n",
    "    def talk(self):\n",
    "        print('身高：' + str(self.height), \", 体重：\" + str(self.weight))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() missing 2 required positional arguments: 'height' and 'weight'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-68-04e9fd7df2ea>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbird\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mBird\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: __init__() missing 2 required positional arguments: 'height' and 'weight'"
     ]
    }
   ],
   "source": [
    "bird = Bird()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "bird = Bird(23,29)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23 29\n"
     ]
    }
   ],
   "source": [
    "print(bird.height,bird.weight)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "emmmmm....\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(bird.cry())  # None 是cry()操作没有返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "身高：23 , 体重：29\n"
     ]
    }
   ],
   "source": [
    "bird.talk()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类初始化传参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Phone(object):\n",
    "    def __init__(self,title,weight,cpu):\n",
    "        self.title = title\n",
    "        self.weight = weight\n",
    "        self.cpu = cpu\n",
    "    def call(self):\n",
    "        print('品牌：', self.title,  '整机重量：', self.weight, '核心数：', self.cpu)\n",
    "    def test_game(self, game):\n",
    "        if self.cpu > game:\n",
    "            print(self.title,'高帧数运行')\n",
    "        elif self.cpu == game:\n",
    "            print(self.title,'流畅运行')\n",
    "        else:\n",
    "            print(self.title,'低帧数运行')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "xm = Phone('小米6', 10, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "品牌： 小米6 整机重量： 10 核心数： 4\n"
     ]
    }
   ],
   "source": [
    "xm.call()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小米6 高帧数运行\n"
     ]
    }
   ],
   "source": [
    "xm.test_game(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小米6 低帧数运行\n"
     ]
    }
   ],
   "source": [
    "xm.test_game(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小米6 流畅运行\n"
     ]
    }
   ],
   "source": [
    "xm.test_game(4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
